﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Configuration;

namespace Tracker.Helper
{
    /// <summary>
    /// Modify connection string and appsetting in Configuration file.
    /// </summary>

    public class AppSetting
    {
        private string _filePath = Directory.GetCurrentDirectory() 
            + @"\Tracker.exe";

        public AppSetting(string filePath)
        {
            _filePath = filePath;
        }

        public AppSetting()
        { }

        public string FilePath
        {   
            get
            { return _filePath; }
        }
        
        public void SaveConnectionString(string key, string val)
        {
            
            var connsec = ConfigurationManager.OpenExeConfiguration(FilePath);
            connsec.ConnectionStrings.ConnectionStrings.Remove(key);
            connsec.ConnectionStrings.ConnectionStrings
                .Add(new ConnectionStringSettings(key, val));
            connsec.Save(ConfigurationSaveMode.Modified);
        
        }

        public void SaveSaveAppSetting(string key, string val)
        {

            var connsec = ConfigurationManager.OpenExeConfiguration(FilePath);
            connsec.AppSettings.Settings.Remove(key);
            connsec.AppSettings.Settings
                .Add(new KeyValueConfigurationElement(key, val));
            connsec.Save(ConfigurationSaveMode.Modified);

        }

        public string GetAppsetting(string key)
        {
            return ConfigurationManager.AppSettings[key] as string;
        }

        public  void SaveTrackerConnection(string conn)
        {
            SaveConnectionString("tracker", conn);
        }

        
    }
}

